//
//  MoyaProvider+Ex.swift
//  
//
//  Created by tank on 2021/11/15.
//

import Foundation
import RxSwift
import Moya
import RxMoya

public extension Reactive where Base: MoyaProviderType {
    func requestEx(_ token: Base.Target, callbackQueue: DispatchQueue? = nil) -> Single<Response> {
        let maxRequestCount: Int = 3
        
        return request(token, callbackQueue: callbackQueue)
            .retry { HttpService.shared.retryRequest(error: $0, maxRetryCount: maxRequestCount) }
            .filterSuccessfulStatusCodes()
            .retry { HttpService.shared.refreshToken(error: $0) }
    }
    
    func requestWithProgressEx(_ token: Base.Target, callbackQueue: DispatchQueue? = nil) -> Observable<ProgressResponse> {
        let maxRequestCount: Int = 3
        
        return requestWithProgress(token, callbackQueue: callbackQueue)
            .retry { HttpService.shared.retryRequest(error: $0, maxRetryCount: maxRequestCount) }
            .filterSuccessfulStatusCodes()
            .retry { HttpService.shared.refreshToken(error: $0) }
    }
}
